import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DELL
 * Date: 2022-09-23
 * Time: 18:58
 */
public class SingleLinkedList {

    //  定义一个节点包含val值和next地址
    class ListNode{
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }

    }


    //定义一个头节点
    public ListNode head;

    public void addFirst(int data) {

        ListNode cur = new ListNode(data);
        cur.next = this.head;
        this.head = cur;
    }


    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if(this.head==null){
            head = node;
        }else {
            while(cur.next!=null){
                cur = cur.next;
            }
            cur.next=node;
        }

    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){

        if (index < 0 || index > size()) {
            throw new IndextIlligelException("index 位置不合法！");
        }
        //头插
        if(index == 0){
            addFirst(data);
            return;
        }
        //尾插
        if(index==size()){
            addLast(data);
            return;
        }

        //其他位置
        ListNode node = new ListNode(data);
        ListNode cur = findindex(index);

        node.next = cur.next;
        cur.next = node;
    }

    private ListNode findindex(int index){
        ListNode cur = this.head;
        while(index--!=0){
            cur = cur.next;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode cur = this.head;
        if(head==null){
            throw new EmptyException("链表为空！");
        }
        while(cur!=null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        //空链表
        if(this.head == null) return;

        //要删除的key出现在第一个节点
        if(this.head.val == key){
            //修改头节点指向
            this.head = this.head.next;
            return;
        }

        //出现在头节点以外的其他地方--寻找key出现的上一个节点
        ListNode cur = findPrevKey(key);
        if(cur==null){
            System.out.println("不存在值为"+key+"的节点！");
            return;
        }
        ListNode del = cur.next;
        cur.next = del.next;
        System.out.println("删除成功！");
    }

    private ListNode findPrevKey(int key){
        ListNode cur = this.head;

        //如果条件为 cur!=null 当寻找的key不存在是，指向最后一个节点时，cur.next 此时为空 cur.next.val 空指针异常
        while(cur.next!=null){
            if(cur.next.val == key){
                return cur;
            }
            cur = cur.next;
        }

        //都没有找到key的节点
        return null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null) return;
        //删除头节点的key -- 放在前面用while删除从头节点开始的多个连续key
        while(this.head.val == key){
            this.head = this.head.next;
        }

        ListNode prev = head;
        ListNode cur = head.next;
        while(cur!=null){
            if(cur.val==key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur =cur.next;
            }
        }

        //删除头节点的key--放在最后删除中间删除后剩下的头节点的key
       /* if(this.head.val == key){
            this.head = this.head.next;
        }*/
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNode cur = this.head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    public void display(){
        ListNode cur = this.head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //递归逆序打印
    public void display2(ListNode head){
        if(head == null){
            return;
        }
        //只有一个节点
        if(head.next == null){
            System.out.print(head.val+" ");
            return;
        }

        display2(head.next);
        System.out.print(head.val+" ");

    }

    public void display3(ListNode head){
        if(head == null){
            return;
        }
        Stack<ListNode> stack = new Stack<>();

        //将节点压栈
        ListNode cur = head;
        while(cur != null){
            stack.push(cur);
            cur = cur.next;
        }

        //将节点得值弹出
        while(!stack.empty()){
            ListNode ret = stack.pop();
            System.out.println(ret.val);
        }
    }

    public void clear(){
        this.head = null;
    }

}
